import Bezier from "./bezier";

var GameData = require('../module/gameData')();
var fishfactory = require('../module/fishfactory')();

cc.Class({
    extends: cc.Component,
    onLoad: function () {
        cc.director.getCollisionManager().enabled = true;
        // cc.director.getCollisionManager().enabledDebugDraw = true;
    },
    onCollisionEnter: function (other, self) {
        var _Allfish = other.node.parent.parent.parent;
        var that = this;
        if (other.node.name != self.node.name) {
            if (self.node.is_check && _Allfish.ischeck) {
                let num = GameData.attack - _Allfish.ph;
                let obj = _Allfish.getChildByName("Allfish").getChildByName("blood");
                /*血条*/

                GameData.GameLayer.comboCount++;
                /*连击数加一*/

                if (num >= 0) {
                    /*将所有与被打中的鱼id相同的新鱼图标去掉*/
                    if (_Allfish.getChildByName("newfish").active) {
                        for (var a = 0; a < GameData.GameLayer.fishArr.length; a++) {
                            if (GameData.GameLayer.fishArr[a].id == _Allfish.id) {
                                GameData.GameLayer.fishArr[a].getChildByName("newfish").active = false;
                            }
                        }
                    }

                    _Allfish.moveStop = true;
                    _Allfish.ischeck = false;
                    _Allfish.eatMove = false;
                    _Allfish.backMove = false;
                    _Allfish.stopAllActions();

                    self.node.stopAllActions();
                    self.node.is_check = false;

                    // that.countCashFish(_Allfish);

                    obj.getChildByName("bloodmask").width = 0;
                    that.judgeFishId(_Allfish, self.node);

                    GameData.GameLayer.removeObjWithArr(GameData.GameLayer.fishArr, _Allfish);

                    console.log("数组", GameData.GameLayer.fishArr.length);
                    if (_Allfish.id != 16) {
                        var pos11 = _Allfish.parent.convertToWorldSpaceAR(_Allfish.getPosition());
                        var _pos1 = self.node.convertToNodeSpaceAR(pos11);

                        let _rota = _Allfish.rotation;
                        GameData.GameLayer.catchFish.push(_Allfish);
                        _Allfish.parent = self.node;
                        _Allfish.setPosition(_pos1.x, _pos1.y);
                        _Allfish.rotation = _rota - self.node.rotation;
                        if (_Allfish.id == 12 || _Allfish.id == 13 || _Allfish.id == 14) {
                            GameData.playEffect(GameData.GameLayer.moveMusic[6]);
                            that.node.stopAllActions();
                            that.node.runAction(
                                cc.sequence(
                                    cc.spawn(
                                        cc.delayTime(0.5),
                                        cc.callFunc(function () {
                                            var boom = cc.instantiate(GameData.GameLayer.boom);
                                            boom.parent = GameData.GameLayer.node;
                                            boom.position = self.node.position;
                                            boom.getComponent(dragonBones.ArmatureDisplay).playAnimation("DD", 0);
                                            boom.runAction(cc.sequence(
                                                cc.moveTo(0.5, GameData.GameLayer.point1.node.x, GameData.GameLayer.point1.node.y),
                                                cc.callFunc(function () {
                                                    boom.destroy();
                                                })
                                            ))
                                        })
                                    ),
                                    cc.callFunc(function () {
                                        GameData.GameLayer.back_bullet(self.node);
                                    })
                                )
                            )
                        } else {
                            GameData.playEffect(GameData.GameLayer.moveMusic[3]);
                            _Allfish.getChildByName("Allfish").getChildByName("fishtype").getChildByName("fish").getComponent(dragonBones.ArmatureDisplay).playAnimation("SW", 1);
                            GameData.GameLayer.back_bullet(self.node);
                        }
                    }
                }
                else {
                    GameData.playEffect(GameData.GameLayer.moveMusic[4]);
                    self.node.stopAllActions();
                    _Allfish.isgo = false;
                    /*表示攻击力小于血量 则鱼没死*/
                    _Allfish.ph = _Allfish.ph - GameData.attack;
                    _Allfish.ph = -num;
                    let w = Math.floor((_Allfish.ph / _Allfish.maxph) * obj.getChildByName("bloodbg").width);
                    obj.getChildByName("bloodmask").width = w;
                    GameData.GameLayer.back_bullet(self.node);
                    obj.active = true;
                    that.runAct(obj);
                }
            }
        }
    },
    // onCollisionEnter1: function (other, self) {
    //     var _Allfish = other.node.parent.parent.parent;
    //     var that = this;
    //     if (other.node.name != self.node.name) {
    //         if (self.node.is_check && _Allfish.ischeck) {
    //             let num = GameData.attack - _Allfish.ph;
    //             // GameData.is_check = false;
    //             let obj = _Allfish.getChildByName("Allfish").getChildByName("blood");
    //             if (num >= 0) {
    //                 // _Allfish.isgo = true;
    //                 _Allfish.moveStop = true;
    //                 _Allfish.ischeck = false;
    //                 if (GameData.GameLayer.bulletType == 1) {
    //                     self.node.stopAllActions();
    //                     self.node.is_check = false;
    //                 }
    //                 self.node.stopAllActions();
    //                 _Allfish.clickPos = _Allfish.getPosition();
    //                 var rota = self.node.rotation;
    //                 var _x = 100 * Math.cos(rota * 3.14 / 180);
    //                 var _y = 100 * Math.sin(rota * 3.14 / 180);
    //                 var _t = 100 / 900;
    //                 if (rota < 0) {
    //                     _y = Math.abs(_y);
    //                 } else {
    //                     _y = -Math.abs(_y);
    //                 }
    //                 _x = Math.abs(_x);
    //
    //                 console.log("数组",GameData.GameLayer.fishArr.length);
    //                 obj.getChildByName("bloodmask").width = 0;
    //                 that.judgeFishId(_Allfish);
    //                 // var point = cc.instantiate(GameData.GameLayer.ropePoint);
    //                 // point.parent=self.node;
    //                 var pos11 = _Allfish.parent.convertToWorldSpaceAR(_Allfish.clickPos);
    //                 var _pos1 = self.node.convertToNodeSpaceAR(pos11);
    //                 // var _pos1 = point.convertToNodeSpaceAR(pos11);
    //                 let _rota = _Allfish.rotation;
    //                 GameData.GameLayer.catchFish.push(_Allfish);
    //                 _Allfish.parent = self.node;
    //                 // _Allfish.parent = point;
    //                 _Allfish.setPosition(_pos1.x, _pos1.y);
    //                 // _Allfish.setPosition(0, 0);
    //                 _Allfish.rotation = _rota - self.node.rotation;
    //                 if (_Allfish.id == 12 || _Allfish.id == 13 || _Allfish.id == 14) {
    //                     that.node.runAction(
    //                         cc.sequence(
    //                             cc.delayTime(0.6),
    //                             cc.callFunc(function () {
    //                                 GameData.GameLayer.back_bullet(self.node);
    //                             })
    //                         )
    //                     )
    //                 } else {
    //                     _Allfish.getChildByName("Allfish").getChildByName("fishtype").getChildByName("fish").getComponent(dragonBones.ArmatureDisplay).playAnimation("SW", 1);
    //                     GameData.GameLayer.back_bullet(self.node);
    //                 }
    //                 // if (GameData.GameLayer.bulletType == 1) {
    //                 //     self.node.runAction(
    //                 //         cc.sequence(
    //                 //             cc.moveBy(_t, _x, _y),
    //                 //             cc.callFunc(function () {
    //                 //                 _Allfish.isgo = false;
    //                 //                 _Allfish.isback = true;
    //                 //                 self.node.is_check = false;
    //                 //                 if (_Allfish.id == 12 || _Allfish.id == 13 || _Allfish.id == 14) {
    //                 //                     that.node.runAction(
    //                 //                         cc.sequence(
    //                 //                             cc.delayTime(0.6),
    //                 //                             cc.callFunc(function () {
    //                 //                                 GameData.GameLayer.back_bullet(self.node);
    //                 //                             })
    //                 //                         )
    //                 //                     )
    //                 //                 } else {
    //                 //                     _Allfish.getChildByName("Allfish").getChildByName("fishtype").getChildByName("fish").getComponent(dragonBones.ArmatureDisplay).playAnimation("SW", 1);
    //                 //                     GameData.GameLayer.back_bullet(self.node);
    //                 //                 }
    //                 //             })
    //                 //         )
    //                 //     )
    //                 // } else {
    //                 //
    //                 // }
    //             }
    //             else {
    //                 self.node.stopAllActions();
    //                 _Allfish.isgo = false;
    //                 /*表示攻击力小于血量 则鱼没死*/
    //                 _Allfish.ph = _Allfish.ph - GameData.attack;
    //                 _Allfish.ph = -num;
    //                 let w = Math.floor((_Allfish.ph / _Allfish.maxph) * obj.getChildByName("bloodbg").width);
    //                 obj.getChildByName("bloodmask").width = w;
    //                 GameData.GameLayer.back_bullet(self.node);
    //                 obj.active = true;
    //                 that.runAct(obj);
    //             }
    //         }
    //     }
    // },
    /*
    * desc: 根据鱼的id判断 不同鱼id对应的不同结果
    * author: <zhouzhiwei>
    * @param:
    * date: 2018/11/23
    */
    judgeFishId(obj, _fish) {
        for (let a = 0; a < GameData.GameLayer.fristIdArr[1].length; a++) {
            if (GameData.GameLayer.fristIdArr[1][a] == obj.id) {
                GameData.GameLayer.fristIdArr[1].splice(a, -1);
                break;
            }
        }
        let _obj = obj.getChildByName("Allfish").getChildByName("fishtype").getChildByName("fish").getComponent(dragonBones.ArmatureDisplay);
        let id = obj.id;
        switch (id) {
            case 10:
                GameData.GameLayer.changeLife(4);
                break;
            case 11:
                GameData.GameLayer.changeLife(8);
                break;
            /*水母 如果打中水母 则玩家会扣对应的血量*/
            case 12:/*扣15血量*/
                _obj.playAnimation("hit", 2);
                GameData.GameLayer.changeLife(-1);
                break;
            case 13:/*扣25血量*/
                _obj.playAnimation("hit", 2);
                GameData.GameLayer.changeLife(-2);
                break;
            case 14:/*扣40血量*/
                _obj.playAnimation("hit", 2);
                GameData.GameLayer.changeLife(-3);
                break;
            case 15:/* 奖励鱼  直接奖励300金币*/
                let getlocakdata = GameData.getlocalData("fishData");
                getlocakdata.coinNum += 300;
                GameData.GameLayer.coinNum.node.parent.getComponent("gamecoin").coinAddEffect(getlocakdata.coinNum, getlocakdata.coinNum + 300);
                GameData.savelocalData(getlocakdata);
                break;
            case 16:/*传染鱼  所以鱼变成奖励鱼*/
                let _Arr = GameData.GameLayer.fishArr;
                GameData.GameLayer.bg.active = true;
                GameData.GameLayer.bg.opacity = 155;
                GameData.isPause = true;
                obj.parent = GameData.GameLayer.bg;
                GameData.GameLayer.updateDrawrope();
                var _pos = obj.position;
                var linArr = [];
                obj.getChildByName("Allfish").getChildByName("fishtype").getChildByName("fish").getComponent(dragonBones.ArmatureDisplay).playAnimation("SW", 1);
                for (let a = 0; a < _Arr.length; a++) {
                    _Arr[a].getChildByName("Allfish").getChildByName("fishtype").getChildByName("fish").getComponent(dragonBones.ArmatureDisplay).playAnimation("", 1);
                    if (GameData.puTongyuId.indexOf(_Arr[a].id) != -1) {
                        linArr.push(_Arr[a]);
                    }
                }
                for (let a = 0; a < linArr.length; a++) {
                    _Arr[a].parent = GameData.GameLayer.bg;
                    var _node = cc.instantiate(GameData.GameLayer.contagion);
                    _node.parent = GameData.GameLayer.bg;
                    _node.position = _pos;
                    var pos1 = {
                        obj1: _Arr[a],
                        pos: _Arr[a].position,
                        idx: a,
                        len: linArr.length
                    }
                    var that = this;
                    var fun = (function (obj) {
                        _node.runAction(
                            cc.sequence(
                                cc.moveTo(0.8, obj.pos.x, obj.pos.y),
                                cc.callFunc(function () {
                                    _node.destroy();
                                    that.restFish(obj.obj1, obj.idx, obj.len, _fish);
                                })
                            )
                        )
                    })(pos1);
                }

                break;
        }
    },
    restFish(obj, idx, len, rope) {

        var pos = obj.position;
        let _fishPrefab = GameData.GameLayer.fishprefab[15];
        var _fish = cc.instantiate(_fishPrefab);
        let mess = GameData.getData(GameData.hashData[0], 15);
        _fish.id = 15;
        _fish.ph = mess.ph;
        _fish.maxph = mess.maxph;
        _fish.price = 300;
        _fish.time = obj.time;
        _fish.moveArr = obj.moveArr;
        _fish.moveid = obj.moveid;
        _fish.movidx = obj.movidx;
        _fish.moveMaxidx = obj.moveMaxidx;
        _fish.runTime = obj.runTime;
        _fish.bezierid = obj.bezierid;
        _fish.lastPoint = obj.lastPoint;
        _fish.cr_fish = true;
        _fish.position = pos;
        _fish.backMove = obj.backMove;
        _fish.iseat = obj.iseat;

        _fish.sceneType = obj.sceneType;
        _fish.ischarged = obj.ischarged;
        // _fish.moveStop = obj.moveStop;
        _fish.moveStop = true;


        _fish.ischeck = obj.ischeck;
        _fish.isgo = obj.isgo;
        _fish.clickPos = obj.clickPos;
        _fish.playXY = obj.playXY;

        GameData.fish_Pool.put(obj);
        obj = _fish;
        obj.moveStop = false;
        _fish.parent = GameData.GameLayer.node;
        if (idx == len - 1) {
            GameData.GameLayer.bg.active = false;
            GameData.GameLayer.bg.opacity = 255;
            GameData.isPause = false;
            GameData.GameLayer.back_bullet(rope);
        }
    },
    runAct(obj) {
        obj.runAction(
            cc.sequence(
                cc.fadeIn(0.1),
                cc.delayTime(0.4),
                cc.fadeOut(0.3)
            )
        )
    },
    /*
    * desc: 统计所捕获的鱼
    * author: <zhouzhiwei>
    * @param:
    * date: 2018/11/26
    */
    countCashFish(obj) {
        let idx = null;
        for (var a = 0; a < GameData.overData.length; a++) {
            if (GameData.overData[a].id == obj.id) {
                idx = a;
                break;
            }
        }
        if (idx != null) {
            GameData.overData[idx].num += 1;
            GameData.overData[idx].price = GameData.overData[idx].num * obj.price
        } else {
            if (typeof obj.id=="number") {
                let _fish = {
                    num: 1,
                    id: obj.id,
                    price: obj.price
                };
                GameData.overData.push(_fish);
            }
        }
        console.log("捕捉鱼信息", GameData.overData)
    },

    onCollisionStay: function (other, self) {

        // console.log('onCollisionStay');
    },

    onCollisionExit: function () {
        // console.log("onCollisionExit");
    },

    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {
    //
    //
    // },
});
